package com.gsbelarus.gedemin.salary.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import com.afollestad.materialdialogs.MaterialDialog;
import com.gsbelarus.gedemin.salary.R;

public class PrefFragment extends PreferenceFragment {

    protected Context context;

    public interface OnPositiveListener {
        void onPositive();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity().getBaseContext();
    }

    protected Preference initListSummary(Preference preference, String[] items, int defaultItem) {
        preference.setSummary(items[getInt(preference.getKey(), defaultItem)]);
        return preference;
    }

    protected void showListDialog(final Preference preference, String title,
                                  String negativeText, final String[] items,
                                  int defaultValue, final OnItemListChanged onItemListChanged) {
        MaterialDialog dialog = new MaterialDialog.Builder(getActivity())
                .items(items)
                .title(title)
                .negativeText(negativeText)
                .itemsCallbackSingleChoice(getInt(preference.getKey(), defaultValue), new MaterialDialog.ListCallbackSingleChoice() {
                    @Override
                    public boolean onSelection(MaterialDialog materialDialog, View view, int i, CharSequence charSequence) {
                        putInt(preference.getKey(), i);
                        preference.setSummary(items[i]);
                        if (onItemListChanged != null)
                            onItemListChanged.onItemListChanged(items[i], i);
                        return false;
                    }
                })
                .build();
        dialog.show();
    }

    protected interface OnItemListChanged {
        void onItemListChanged(String value, int position);
    }

    protected Preference initEditSummary(Preference preference, String defaultValue) {
        String summary = getString(preference.getKey(), defaultValue);
        if (summary.isEmpty()) summary = defaultValue;
        preference.setSummary(summary);
        return preference;
    }

    protected void showEditDialog(final Preference preference, String title,
                                  String positiveText, String negativeText, InputFilter filter,  final OnPositiveListener listener) {
        ViewGroup layout = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.dialog_edit_text, null);
        final EditText editText = (EditText) layout.findViewById(android.R.id.edit);
        if (filter != null) {
            editText.setFilters(new InputFilter[]{filter});
        }
        editText.setText(getString(preference.getKey(), ""));
        editText.setSelection(editText.length());
        MaterialDialog dialog = new MaterialDialog.Builder(getActivity())
                .title(title)
                .positiveText(positiveText)
                .negativeText(negativeText)
                .customView(layout, false)
                .callback(new MaterialDialog.ButtonCallback() {
                    @Override
                    public void onPositive(MaterialDialog dialog) {
                        super.onPositive(dialog);
                        if (editText.getText().toString().isEmpty())
                            preference.setSummary(R.string.not_specified_text);
                        else
                            preference.setSummary(editText.getText());
                        putString(preference.getKey(), editText.getText().toString());
                        if (listener != null)
                            listener.onPositive();
                    }
                })
                .build();
        dialog.show();

        Window window = dialog.getWindow();
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }

    protected MaterialDialog showAlertDialog(Spanned title, String message,
                                             String positiveButtonText, String negativeButtonText, String neutralButtonText,
                                             MaterialDialog.ButtonCallback callback) {
        MaterialDialog.Builder dialogBuilder = new MaterialDialog.Builder(getActivity());
        if (title != null)
            dialogBuilder.title(title);
        if (message != null)
            dialogBuilder.content(message);
        if (positiveButtonText != null)
            dialogBuilder.positiveText(positiveButtonText);
        if (negativeButtonText != null)
            dialogBuilder.negativeText(negativeButtonText);
        if (neutralButtonText != null)
            dialogBuilder.neutralText(neutralButtonText);
        if (callback != null)
            dialogBuilder.callback(callback);

        MaterialDialog dialog = dialogBuilder.build();
        if (getActivity().isFinishing()) return null;
        dialog.show();

        return dialog;
    }

    protected void putString(String prefKey, String value) {
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
        editor.putString(prefKey, value);
        editor.commit();
    }

    protected void putInt(String prefKey, int value) {
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
        editor.putInt(prefKey, value);
        editor.commit();
    }

    protected void putBoolean(String prefKey, boolean value) {
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
        editor.putBoolean(prefKey, value);
        editor.commit();
    }

    protected String getString(String prefKey, String defaultValue) {
        return PreferenceManager.getDefaultSharedPreferences(context).getString(prefKey, defaultValue);
    }

    protected int getInt(String prefKey, int defaultValue) {
        return PreferenceManager.getDefaultSharedPreferences(context).getInt(prefKey, defaultValue);
    }

    protected boolean getBoolean(String prefKey, boolean defaultValue) {
        return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(prefKey, defaultValue);
    }
}
